/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.mathematica;

/**
 *
 * @author Mr.Sam
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import fr.ove.utils.Connection;

/**
* A Status Bar
*/
public class StatusBar extends JPanel implements ChangeListener {
    /**
    * The status to display.
    */
    private JLabel status;
    
    /**
    * The connection status.
    */
    private JLabel connStatus;
    
    /**
    * A blank icon.
    */
    private ImageIcon blank;
    
    /**
    * The connexion satus : online
    */
    private ImageIcon online;
    
    /**
    * The connexion satus : offline
    */
    private ImageIcon offline;
    
    /**
    * A connection handler dialog
    */
    public ConnectionHandlerDialog connectionHandlerDialog;
    
    /**
    * The constructor.
    */
    public StatusBar(JFrame parent) {
        super(new GridBagLayout());
        
        connectionHandlerDialog = new ConnectionHandlerDialog(parent, this);
        
        BevelBorder border = new BevelBorder(BevelBorder.LOWERED);
        
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.weightx = 1.0;
        constraints.anchor = GridBagConstraints.WEST;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.ipady = 2;
        
        blank = getIcon("fr/ove/openmath/mathematica/images/blank.gif");
        status = new JLabel(blank);
        status.setHorizontalAlignment(JLabel.LEFT);
        status.setBorder(border);
        status.setIconTextGap(5);
        add(status, constraints);
        
        constraints.gridx = 1;
        constraints.weightx = 0.0;
        constraints.anchor = GridBagConstraints.CENTER;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.ipadx = 10;
        
        connStatus = new JLabel(blank);
        connStatus.setToolTipText("");
        connStatus.addMouseListener(
            new MouseAdapter() {
                public void mousePressed(MouseEvent me) {
                    if (!connStatus.getToolTipText().equals("")) {
                        connectionHandlerDialog.show();
                    }
                }
            }
        );
        connStatus.setHorizontalAlignment(JLabel.CENTER);
        connStatus.setBorder(border);
        add(connStatus, constraints);
        
        online = getIcon("fr/ove/openmath/mathematica/images/online.gif");
        offline = getIcon("fr/ove/openmath/mathematica/images/offline.gif");
    }
    
    /**
    * Sets the text for the status.
    * @param the tex for the status.
    */
    public void setStatus(String text) {
        status.setText(text);
        status.setHorizontalAlignment(JLabel.LEFT);
    }
    
    /**
    * Sets the status.
    */
    public void setStatus(String text, ImageIcon icon) {
        status.setText(text);
        status.setIcon(icon);
        status.setHorizontalAlignment(JLabel.LEFT);
    }
    
    /**
    * Clears the status.
    */
    public void clear() {
        setStatus("", blank);
    }
    
    /**
    * Sets the connection status.
    * @param state <CODE>true</CODE> if state. <CODE>false</CODE> otherwise.
    */
    public void setConnectionStatus(boolean state) {
        if (state) {
            connStatus.setIcon(online);
            connStatus.setToolTipText("Connected to the server");
        }
        else {
            connStatus.setIcon(offline);
            connStatus.setToolTipText("Not connected to the server");
        }
    }
    
    /**
    * Returns the icon with the specified image filename.
    * @param filename the image filename
    */
    private ImageIcon getIcon(String filename) {
        ImageIcon icon = null;
        
        java.net.URL iconURL = ClassLoader.getSystemResource(filename);
        if (iconURL != null)
            icon = new ImageIcon(iconURL);
            
        return icon;
    }
    
    //  ### ChangeListenerInterface implementation
    
    /**
    * Invoked when the target of the listener has changed its state.
    * @param e a ChangeEvent object 
    */
    public void stateChanged(ChangeEvent e) {
        WorkBook workBook = (WorkBook) e.getSource();
        
        if (workBook.getTabCount() == 0) {
            clear();
            connStatus.setIcon(blank);
            connStatus.setToolTipText("");
        }
        else {
            setStatus("", getIcon("fr/ove/openmath/mathematica/images/worksheet.gif"));
            
            JInternalFrame jif = workBook.getActiveInternalFrame();
            OMWorkSheet workSheet = (OMWorkSheet) jif.getContentPane();
            Connection connection = workSheet.getConnection();
            
            setConnectionStatus(connection.isOpened());
            connectionHandlerDialog.setConnection(connection);
        }
    }
}